package com.demo.thread.atomicInteger;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author peter
 * @date 2022/3/16 上午1:09
 */
public class CasDemo {

    /**
     * CAS 场景认识
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("================CAS compareAndSet 类似乐观锁==========================");

        System.out.println("设置 atomicInteger 为123");
        AtomicInteger atomicInteger = new AtomicInteger(123);

        int expectedValue = 123;// 期待值
        int newValue      = 234;// 更新值

        System.out.println("atomicInteger 123 expectedValue 123 相等, 于是newValue更新成功, atomicInteger 等于234");
        boolean b =atomicInteger.compareAndSet(expectedValue, newValue);

        System.out.println("是否更新成功：" + Boolean.toString(b));
        System.out.println("更新后:" + String.valueOf(atomicInteger));
    }
}
